<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/1.png" style="height:60px;width:64px" alt=""/>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollp">
          |||
        </div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened
          :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :key="item.id" v-for="item in menulist" :index="item.id + ''">
            <template slot="title">
              <!-- <i :class="iconsobj['item.id']"></i> -->
              <i :class="iconsobj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="saveNavState('/'+subItem.path)" :key="subItem.id" v-for="subItem in item.children" :index="'/'+subItem.path">
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主题内容区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      iconsobj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-claim',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-shop',
        145: 'el-icon-s-marketing'
      },
      //   侧边栏默认不折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  // 生命周期函数
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    //   点击退出按钮，回到登录页面
    logout () {
      // 清空sessionStorage里面用户账号信息
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单方法
    async getMenuList () {
      const {
        data
      } = await this.$http.get('menus')
      // console.log(data)
      if (data.meta.status !== 200) return this.$message.error(data.meta.msg)
      this.menulist = data.data
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollp () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}

</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    line-height: 60px;
    display: flex; // 弹性布局
    justify-content: space-between; // 左右两边对齐
    padding-left: 0;
    align-items: center; // 居中对齐弹性盒的各项 <div> 元素
    color: white;
    font-size: 20px;

    div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }

      img {
        opacity: 0.7;
        border-radius: 50%;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    color: white;

    .el-menu {
      border-right: none;
    }

    .toggle-button {
      background-color: #4a5064;
      font-size: 10px;
      color: white;
      text-align: center;
      line-height: 30px;
      letter-spacing: 0.5em;
      cursor: pointer; // 鼠标放上去变成小手
    }
  }

  .el-main {
    background-color: #e9eef3;
    color: black;
  }

</style>
